package com.umbrella.worker.util;
import java.io.UnsupportedEncodingException;  
import java.net.URLEncoder;  
import java.util.*;  
import java.util.Map.Entry;  
  
public class CommonUtil {  
  
    public static String CreateNoncestr(int length) {  
        String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
        String res = "";  
        for (int i = 0; i < length; i++) {  
            Random rd = new Random();  
            res += chars.indexOf(rd.nextInt(chars.length() - 1));  
        }  
        return res;  
    }  
  
    public static String CreateNoncestr() {  
        String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
        String res = "";  
        for (int i = 0; i < 16; i++) {  
            Random rd = new Random();  
            res += chars.charAt(rd.nextInt(chars.length() - 1));  
        }  
        return res;  
    }  
  
    public static String FormatQueryParaMap(HashMap<String, String> parameters)  
            throws Exception {  
  
        String buff = "";  
        try {  
            List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(  
                    parameters.entrySet());  
  
            Collections.sort(infoIds,  
                    new Comparator<Map.Entry<String, String>>() {  
                        public int compare(Map.Entry<String, String> o1,  
                                Map.Entry<String, String> o2) {  
                            return (o1.getKey()).toString().compareTo(  
                                    o2.getKey());  
                        }  
                    });  
  
            for (int i = 0; i < infoIds.size(); i++) {  
                Map.Entry<String, String> item = infoIds.get(i);  
                if (item.getKey() != "") {  
                    buff += item.getKey() + "="  
                            + URLEncoder.encode(item.getValue(), "utf-8") + "&";  
                }  
            }  
            if (buff.isEmpty() == false) {  
                buff = buff.substring(0, buff.length() - 1);  
            }  
        } catch (Exception e) {  
            throw new Exception(e.getMessage());  
        }  
  
        return buff;  
    }  
  
    public static String FormatBizQueryParaMap(Map<String, String> paraMap,  
            boolean urlencode) throws Exception {  
  
        String buff = "";  
        try {  
            List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(  
                    paraMap.entrySet());  
  
            Collections.sort(infoIds,  
                    new Comparator<Map.Entry<String, String>>() {  
                        public int compare(Map.Entry<String, String> o1,  
                                Map.Entry<String, String> o2) {  
                            return (o1.getKey()).toString().compareTo(  
                                    o2.getKey());  
                        }  
                    });  
  
            for (int i = 0; i < infoIds.size(); i++) {  
                Map.Entry<String, String> item = infoIds.get(i);  
                //System.out.println(item.getKey());  
                if (item.getKey() != "") {     
                    String key = item.getKey();  
                    String val = item.getValue();  
                    if (urlencode) {  
                        val = URLEncoder.encode(val, "utf-8");  
  
                    }  
                    buff += key+ "=" + val + "&";  
  
                }  
            }  
  
            if (buff.isEmpty() == false) {  
                buff = buff.substring(0, buff.length() - 1);  
            }  
        } catch (Exception e) {  
            throw new Exception(e.getMessage());  
        }  
        return buff;  
    }  
  
    public static boolean IsNumeric(String str) {  
        if (str.matches("\\d *")) {  
            return true;  
        } else {  
            return false;  
        }  
    }  
  
    public static String ArrayToXml(Map<String, String> arr) {  
        String xml = "<xml>";  
          
        Iterator<Entry<String, String>> iter = arr.entrySet().iterator();  
        while (iter.hasNext()) {  
            Entry<String, String> entry = iter.next();  
            String key = entry.getKey();  
            String val = entry.getValue();  
            if (IsNumeric(val)) {  
                xml += "<" + key + ">" + val + "</" + key + ">";  
  
            } else  
                xml += "<" + key + "><![CDATA[" + val + "]]></" + key + ">";  
        }  
  
        xml += "</xml>";  
         try {  
            return new String(xml.toString().getBytes(),"ISO8859-1");  
        } catch (UnsupportedEncodingException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }    
         return "";  
    }  
  
}  